/*
 * Copyright (C) 2013 tangzhi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.tangzsoft.pdmviewer.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 *  key忽略大小写的HashMap
 * @author tangzhi
 */
public class CaseInsensitiveMap<V> extends HashMap<String,V> {
    
    static final float DEFAULT_LOAD_FACTOR = 0.75f;
    static final int DEFAULT_INITIAL_CAPACITY = 16;
    
    private HashMap<String,V> map;
    
    public CaseInsensitiveMap(int initialCapacity, float loadFactor) {
        map = new HashMap(initialCapacity, loadFactor);
    }
    
    public CaseInsensitiveMap(int initialCapacity) {
        map = new HashMap(initialCapacity);
    }
    
    public CaseInsensitiveMap() {
        map = new HashMap();
    }
    
    public CaseInsensitiveMap(Map<? extends String, ? extends V> m) {
        
        map = new HashMap(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
                      DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
        
        for (Iterator<? extends Map.Entry<? extends String, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry<? extends String, ? extends V> e = i.next();
            map.put(e.getKey().toLowerCase(), e.getValue());
        }
    }
    
    @Override
    public V put(String key, V value) {
        return map.put(key.toLowerCase(), value);
    }
    
    @Override
    public V get(Object key) {
        return map.get(key.toString().toLowerCase());
    }
    
    @Override
    public boolean containsKey(Object key) {
        return map.containsKey(key.toString().toLowerCase());
    }
            
}
